package com.jr.controller;
import com.jr.util.ValidateCodeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@RestController
public class ValidateCodeController {
    //返回验证码图片
    @GetMapping(value = "/getCaptchaImg",produces = "application/json;charset=utf-8;")
    public void getCaptchaImg(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        try {
            response.setContentType("image/png");
            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Expire", "0");
            response.setHeader("Pragma", "no-cache");
            ValidateCodeUtil validateCode = new ValidateCodeUtil();
// getRandomCodeImage方法会直接将生成的验证码图片写入response
            validateCode.getRandomCodeImage(request, response);
//System.out.println("session里面存储的验证码为："+ session.getAttribute("JCCODE"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //验证码校验
    @GetMapping("/checkCaptcha")
    public @ResponseBody String getCheckCaptcha(String code,HttpSession session) {
        String jccode = (String) session.getAttribute("JCCODE");
        if (jccode.toLowerCase().equals(code.toLowerCase())) {
            return "true";
        }
        return "false";
    }
}
